
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>venv --- 创建虚拟环境 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="zipapp --- Manage executable Python zip archives" href="zipapp.html" />
    <link rel="prev" title="ensurepip --- Bootstrapping the pip installer" href="ensurepip.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/venv.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="zipapp.html" title="zipapp --- Manage executable Python zip archives"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="ensurepip.html" title="ensurepip --- Bootstrapping the pip installer"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="distribution.html" accesskey="U">软件打包和分发</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-venv">
<span id="venv-creation-of-virtual-environments"></span><h1><a class="reference internal" href="#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> --- 创建虚拟环境<a class="headerlink" href="#module-venv" title="永久链接至标题">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<p><strong>源码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/venv/">Lib/venv/</a></p>
<hr class="docutils" id="index-0" />
<p><a class="reference internal" href="#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 模块支持使用自己的站点目录创建轻量级“虚拟环境”，可选择与系统站点目录隔离。每个虚拟环境都有自己的 Python 二进制文件（与用于创建此环境的二进制文件的版本相匹配），并且可以在其站点目录中拥有自己独立的已安装 Python 软件包集。</p>
<p>有关 Python 虚拟环境的更多信息，请参阅 <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0405"><strong>PEP 405</strong></a> 。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><a class="reference external" href="https://packaging.python.org/installing/#creating-virtual-environments">Python 软件包用户指南：创建和使用虚拟环境</a></p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>从 Python 3.6 开始，不推荐使用 <code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> 脚本，而是使用 <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-m</span> <span class="pre">venv</span></code> 来帮助防止任何关于虚拟环境将基于哪个 Python 解释器的混淆。</p>
</div>
<div class="section" id="creating-virtual-environments">
<h2>创建虚拟环境<a class="headerlink" href="#creating-virtual-environments" title="永久链接至标题">¶</a></h2>
<p>通过执行 <code class="docutils literal notranslate"><span class="pre">venv</span></code> 指令来创建一个 <a class="reference internal" href="#venv-def"><span class="std std-ref">虚拟环境</span></a>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python3</span> <span class="o">-</span><span class="n">m</span> <span class="n">venv</span> <span class="o">/</span><span class="n">path</span><span class="o">/</span><span class="n">to</span><span class="o">/</span><span class="n">new</span><span class="o">/</span><span class="n">virtual</span><span class="o">/</span><span class="n">environment</span>
</pre></div>
</div>
<p>Running this command creates the target directory (creating any parent
directories that don't exist already) and places a <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file in it
with a <code class="docutils literal notranslate"><span class="pre">home</span></code> key pointing to the Python installation from which the command
was run.  It also creates a <code class="docutils literal notranslate"><span class="pre">bin</span></code> (or <code class="docutils literal notranslate"><span class="pre">Scripts</span></code> on Windows) subdirectory
containing a copy/symlink of the Python binary/binaries (as appropriate for the
platform or arguments used at environment creation time). It also creates an
(initially empty) <code class="docutils literal notranslate"><span class="pre">lib/pythonX.Y/site-packages</span></code> subdirectory
(on Windows, this is <code class="docutils literal notranslate"><span class="pre">Lib\site-packages</span></code>). If an existing
directory is specified, it will be re-used.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.6 版后已移除: </span><code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> 是 Python 3.3 和 3.4 中创建虚拟环境的推荐工具，不过 <a class="reference external" href="https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features">在 Python 3.6 中已弃用</a>。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>现在推荐使用 <code class="docutils literal notranslate"><span class="pre">venv</span></code> 来创建虚拟环境。</p>
</div>
<p>On Windows, invoke the <code class="docutils literal notranslate"><span class="pre">venv</span></code> command as follows:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>c:\&gt;c:\Python35\python -m venv c:\path\to\myenv
</pre></div>
</div>
<p>Alternatively, if you configured the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> and <code class="docutils literal notranslate"><span class="pre">PATHEXT</span></code> variables for
your <a class="reference internal" href="../using/windows.html#using-on-windows"><span class="std std-ref">Python installation</span></a>:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>c:\&gt;python -m venv c:\path\to\myenv
</pre></div>
</div>
<p>The command, if run with <code class="docutils literal notranslate"><span class="pre">-h</span></code>, will show the available options:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)
  --prompt PROMPT       Provides an alternative prompt prefix for this
                        environment.

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>Installs pip by default, added the <code class="docutils literal notranslate"><span class="pre">--without-pip</span></code>  and <code class="docutils literal notranslate"><span class="pre">--copies</span></code>
options</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>In earlier versions, if the target directory already existed, an error was
raised, unless the <code class="docutils literal notranslate"><span class="pre">--clear</span></code> or <code class="docutils literal notranslate"><span class="pre">--upgrade</span></code> option was provided.</p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>While symlinks are supported on Windows, they are not recommended. Of
particular note is that double-clicking <code class="docutils literal notranslate"><span class="pre">python.exe</span></code> in File Explorer
will resolve the symlink eagerly and ignore the virtual environment.</p>
</div>
<p>The created <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> file also includes the
<code class="docutils literal notranslate"><span class="pre">include-system-site-packages</span></code> key, set to <code class="docutils literal notranslate"><span class="pre">true</span></code> if <code class="docutils literal notranslate"><span class="pre">venv</span></code> is
run with the <code class="docutils literal notranslate"><span class="pre">--system-site-packages</span></code> option, <code class="docutils literal notranslate"><span class="pre">false</span></code> otherwise.</p>
<p>Unless the <code class="docutils literal notranslate"><span class="pre">--without-pip</span></code> option is given, <a class="reference internal" href="ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> will be
invoked to bootstrap <code class="docutils literal notranslate"><span class="pre">pip</span></code> into the virtual environment.</p>
<p>Multiple paths can be given to <code class="docutils literal notranslate"><span class="pre">venv</span></code>, in which case an identical virtual
environment will be created, according to the given options, at each provided
path.</p>
<p>Once a virtual environment has been created, it can be &quot;activated&quot; using a
script in the virtual environment's binary directory. The invocation of the
script is platform-specific (<cite>&lt;venv&gt;</cite> must be replaced by the path of the
directory containing the virtual environment):</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 18%" />
<col style="width: 24%" />
<col style="width: 58%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>平台</p></th>
<th class="head"><p>Shell</p></th>
<th class="head"><p>用于激活虚拟环境的命令</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>Posix</p></td>
<td><p>bash/zsh</p></td>
<td><p>$ source &lt;venv&gt;/bin/activate</p></td>
</tr>
<tr class="row-odd"><td></td>
<td><p>fish</p></td>
<td><p>$ . &lt;venv&gt;/bin/activate.fish</p></td>
</tr>
<tr class="row-even"><td></td>
<td><p>csh/tcsh</p></td>
<td><p>$ source &lt;venv&gt;/bin/activate.csh</p></td>
</tr>
<tr class="row-odd"><td><p>Windows</p></td>
<td><p>cmd.exe</p></td>
<td><p>C:\&gt; &lt;venv&gt;\Scripts\activate.bat</p></td>
</tr>
<tr class="row-even"><td></td>
<td><p>PowerShell</p></td>
<td><p>PS C:\&gt; &lt;venv&gt;\Scripts\Activate.ps1</p></td>
</tr>
</tbody>
</table>
<p>You don't specifically <em>need</em> to activate an environment; activation just
prepends the virtual environment's binary directory to your path, so that
&quot;python&quot; invokes the virtual environment's Python interpreter and you can run
installed scripts without having to use their full path. However, all scripts
installed in a virtual environment should be runnable without activating it,
and run with the virtual environment's Python automatically.</p>
<p>You can deactivate a virtual environment by typing &quot;deactivate&quot; in your shell.
The exact mechanism is platform-specific and is an internal implementation
detail (typically a script or shell function will be used).</p>
<div class="versionadded">
<p><span class="versionmodified added">3.4 新版功能: </span><code class="docutils literal notranslate"><span class="pre">fish</span></code> and <code class="docutils literal notranslate"><span class="pre">csh</span></code> activation scripts.</p>
</div>
<div class="admonition note" id="venv-def">
<p class="admonition-title">注解</p>
<p>A virtual environment is a Python environment such that the Python
interpreter, libraries and scripts installed into it are isolated from those
installed in other virtual environments, and (by default) any libraries
installed in a &quot;system&quot; Python, i.e., one which is installed as part of your
operating system.</p>
<p>A virtual environment is a directory tree which contains Python executable
files and other files which indicate that it is a virtual environment.</p>
<p>Common installation tools such as <a class="reference external" href="https://pypi.org/project/setuptools/">setuptools</a> and <a class="reference external" href="https://pypi.org/project/pip/">pip</a> work as
expected with virtual environments. In other words, when a virtual
environment is active, they install Python packages into the virtual
environment without needing to be told to do so explicitly.</p>
<p>When a virtual environment is active (i.e., the virtual environment's Python
interpreter is running), the attributes <a class="reference internal" href="sys.html#sys.prefix" title="sys.prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.prefix</span></code></a> and
<a class="reference internal" href="sys.html#sys.exec_prefix" title="sys.exec_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code></a> point to the base directory of the virtual
environment, whereas <a class="reference internal" href="sys.html#sys.base_prefix" title="sys.base_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.base_prefix</span></code></a> and
<a class="reference internal" href="sys.html#sys.base_exec_prefix" title="sys.base_exec_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.base_exec_prefix</span></code></a> point to the non-virtual environment Python
installation which was used to create the virtual environment. If a virtual
environment is not active, then <a class="reference internal" href="sys.html#sys.prefix" title="sys.prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.prefix</span></code></a> is the same as
<a class="reference internal" href="sys.html#sys.base_prefix" title="sys.base_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.base_prefix</span></code></a> and <a class="reference internal" href="sys.html#sys.exec_prefix" title="sys.exec_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code></a> is the same as
<a class="reference internal" href="sys.html#sys.base_exec_prefix" title="sys.base_exec_prefix"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.base_exec_prefix</span></code></a> (they all point to a non-virtual environment
Python installation).</p>
<p>When a virtual environment is active, any options that change the
installation path will be ignored from all <a class="reference internal" href="distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a> configuration
files to prevent projects being inadvertently installed outside of the
virtual environment.</p>
<p>When working in a command shell, users can make a virtual environment active
by running an <code class="docutils literal notranslate"><span class="pre">activate</span></code> script in the virtual environment's executables
directory (the precise filename and command to use the file is
shell-dependent), which prepends the virtual environment's directory for
executables to the <code class="docutils literal notranslate"><span class="pre">PATH</span></code> environment variable for the running shell. There
should be no need in other circumstances to activate a virtual
environment; scripts installed into virtual environments have a &quot;shebang&quot;
line which points to the virtual environment's Python interpreter. This means
that the script will run with that interpreter regardless of the value of
<code class="docutils literal notranslate"><span class="pre">PATH</span></code>. On Windows, &quot;shebang&quot; line processing is supported if you have the
Python Launcher for Windows installed (this was added to Python in 3.3 - see
<span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0397"><strong>PEP 397</strong></a> for more details). Thus, double-clicking an installed script in a
Windows Explorer window should run the script with the correct interpreter
without there needing to be any reference to its virtual environment in
<code class="docutils literal notranslate"><span class="pre">PATH</span></code>.</p>
</div>
</div>
<div class="section" id="api">
<span id="venv-api"></span><h2>API<a class="headerlink" href="#api" title="永久链接至标题">¶</a></h2>
<p>The high-level method described above makes use of a simple API which provides
mechanisms for third-party virtual environment creators to customize environment
creation according to their needs, the <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class.</p>
<dl class="class">
<dt id="venv.EnvBuilder">
<em class="property">class </em><code class="sig-prename descclassname">venv.</code><code class="sig-name descname">EnvBuilder</code><span class="sig-paren">(</span><em class="sig-param">system_site_packages=False</em>, <em class="sig-param">clear=False</em>, <em class="sig-param">symlinks=False</em>, <em class="sig-param">upgrade=False</em>, <em class="sig-param">with_pip=False</em>, <em class="sig-param">prompt=None</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder" title="永久链接至目标">¶</a></dt>
<dd><p>The <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class accepts the following keyword arguments on
instantiation:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">system_site_packages</span></code> -- a Boolean value indicating that the system Python
site-packages should be available to the environment (defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">clear</span></code> -- a Boolean value which, if true, will delete the contents of
any existing target directory, before creating the environment.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">symlinks</span></code> -- a Boolean value indicating whether to attempt to symlink the
Python binary rather than copying.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">upgrade</span></code> -- a Boolean value which, if true, will upgrade an existing
environment with the running Python - for use when that Python has been
upgraded in-place (defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">with_pip</span></code> -- a Boolean value which, if true, ensures pip is
installed in the virtual environment. This uses <a class="reference internal" href="ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> with
the <code class="docutils literal notranslate"><span class="pre">--default-pip</span></code> option.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">prompt</span></code> -- a String to be used after virtual environment is activated
(defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code> which means directory name of the environment would
be used).</p></li>
</ul>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>Added the <code class="docutils literal notranslate"><span class="pre">with_pip</span></code> parameter</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>Added the <code class="docutils literal notranslate"><span class="pre">prompt</span></code> parameter</p>
</div>
<p>Creators of third-party virtual environment tools will be free to use the
provided <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class as a base class.</p>
<p>The returned env-builder is an object which has a method, <code class="docutils literal notranslate"><span class="pre">create</span></code>:</p>
<dl class="method">
<dt id="venv.EnvBuilder.create">
<code class="sig-name descname">create</code><span class="sig-paren">(</span><em class="sig-param">env_dir</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.create" title="永久链接至目标">¶</a></dt>
<dd><p>Create a virtual environment by specifying the target directory
(absolute or relative to the current directory) which is to contain the
virtual environment.  The <code class="docutils literal notranslate"><span class="pre">create</span></code> method will either create the
environment in the specified directory, or raise an appropriate
exception.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">create</span></code> method of the <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> class illustrates the
hooks available for subclass customization:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">env_dir</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a virtualized Python environment in a directory.</span>
<span class="sd">    env_dir is the target directory to create an environment in.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">env_dir</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="n">env_dir</span><span class="p">)</span>
    <span class="n">context</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ensure_directories</span><span class="p">(</span><span class="n">env_dir</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">create_configuration</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup_python</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">setup_scripts</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">post_setup</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
<p>Each of the methods <a class="reference internal" href="#venv.EnvBuilder.ensure_directories" title="venv.EnvBuilder.ensure_directories"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ensure_directories()</span></code></a>,
<a class="reference internal" href="#venv.EnvBuilder.create_configuration" title="venv.EnvBuilder.create_configuration"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_configuration()</span></code></a>, <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a>,
<a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a> and <a class="reference internal" href="#venv.EnvBuilder.post_setup" title="venv.EnvBuilder.post_setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_setup()</span></code></a> can be overridden.</p>
</dd></dl>

<dl class="method">
<dt id="venv.EnvBuilder.ensure_directories">
<code class="sig-name descname">ensure_directories</code><span class="sig-paren">(</span><em class="sig-param">env_dir</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.ensure_directories" title="永久链接至目标">¶</a></dt>
<dd><p>Creates the environment directory and all necessary directories, and
returns a context object.  This is just a holder for attributes (such as
paths), for use by the other methods. The directories are allowed to
exist already, as long as either <code class="docutils literal notranslate"><span class="pre">clear</span></code> or <code class="docutils literal notranslate"><span class="pre">upgrade</span></code> were
specified to allow operating on an existing environment directory.</p>
</dd></dl>

<dl class="method">
<dt id="venv.EnvBuilder.create_configuration">
<code class="sig-name descname">create_configuration</code><span class="sig-paren">(</span><em class="sig-param">context</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.create_configuration" title="永久链接至目标">¶</a></dt>
<dd><p>Creates the <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code> configuration file in the environment.</p>
</dd></dl>

<dl class="method">
<dt id="venv.EnvBuilder.setup_python">
<code class="sig-name descname">setup_python</code><span class="sig-paren">(</span><em class="sig-param">context</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.setup_python" title="永久链接至目标">¶</a></dt>
<dd><p>Creates a copy or symlink to the Python executable in the environment.
On POSIX systems, if a specific executable <code class="docutils literal notranslate"><span class="pre">python3.x</span></code> was used,
symlinks to <code class="docutils literal notranslate"><span class="pre">python</span></code> and <code class="docutils literal notranslate"><span class="pre">python3</span></code> will be created pointing to that
executable, unless files with those names already exist.</p>
</dd></dl>

<dl class="method">
<dt id="venv.EnvBuilder.setup_scripts">
<code class="sig-name descname">setup_scripts</code><span class="sig-paren">(</span><em class="sig-param">context</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.setup_scripts" title="永久链接至目标">¶</a></dt>
<dd><p>Installs activation scripts appropriate to the platform into the virtual
environment.</p>
</dd></dl>

<dl class="method">
<dt id="venv.EnvBuilder.post_setup">
<code class="sig-name descname">post_setup</code><span class="sig-paren">(</span><em class="sig-param">context</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.post_setup" title="永久链接至目标">¶</a></dt>
<dd><p>A placeholder method which can be overridden in third party
implementations to pre-install packages in the virtual environment or
perform other post-creation steps.</p>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7.2 版更改: </span>Windows now uses redirector scripts for <code class="docutils literal notranslate"><span class="pre">python[w].exe</span></code> instead of
copying the actual binaries. In 3.7.2 only <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a> does
nothing unless running from a build in the source tree.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7.3 版更改: </span>Windows copies the redirector scripts as part of <a class="reference internal" href="#venv.EnvBuilder.setup_python" title="venv.EnvBuilder.setup_python"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_python()</span></code></a>
instead of <a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a>. This was not the case in 3.7.2.
When using symlinks, the original executables will be linked.</p>
</div>
<p>In addition, <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a> provides this utility method that can be
called from <a class="reference internal" href="#venv.EnvBuilder.setup_scripts" title="venv.EnvBuilder.setup_scripts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setup_scripts()</span></code></a> or <a class="reference internal" href="#venv.EnvBuilder.post_setup" title="venv.EnvBuilder.post_setup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_setup()</span></code></a> in subclasses to
assist in installing custom scripts into the virtual environment.</p>
<dl class="method">
<dt id="venv.EnvBuilder.install_scripts">
<code class="sig-name descname">install_scripts</code><span class="sig-paren">(</span><em class="sig-param">context</em>, <em class="sig-param">path</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.EnvBuilder.install_scripts" title="永久链接至目标">¶</a></dt>
<dd><p><em>path</em> is the path to a directory that should contain subdirectories
&quot;common&quot;, &quot;posix&quot;, &quot;nt&quot;, each containing scripts destined for the bin
directory in the environment.  The contents of &quot;common&quot; and the
directory corresponding to <a class="reference internal" href="os.html#os.name" title="os.name"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.name</span></code></a> are copied after some text
replacement of placeholders:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">__VENV_DIR__</span></code> is replaced with the absolute path of the environment
directory.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__VENV_NAME__</span></code> is replaced with the environment name (final path
segment of environment directory).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__VENV_PROMPT__</span></code> is replaced with the prompt (the environment
name surrounded by parentheses and with a following space)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__VENV_BIN_NAME__</span></code> is replaced with the name of the bin directory
(either <code class="docutils literal notranslate"><span class="pre">bin</span></code> or <code class="docutils literal notranslate"><span class="pre">Scripts</span></code>).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">__VENV_PYTHON__</span></code> is replaced with the absolute path of the
environment's executable.</p></li>
</ul>
<p>The directories are allowed to exist (for when an existing environment
is being upgraded).</p>
</dd></dl>

</dd></dl>

<p>有一个方便实用的模块级别的函数:</p>
<dl class="function">
<dt id="venv.create">
<code class="sig-prename descclassname">venv.</code><code class="sig-name descname">create</code><span class="sig-paren">(</span><em class="sig-param">env_dir</em>, <em class="sig-param">system_site_packages=False</em>, <em class="sig-param">clear=False</em>, <em class="sig-param">symlinks=False</em>, <em class="sig-param">with_pip=False</em>, <em class="sig-param">prompt=None</em><span class="sig-paren">)</span><a class="headerlink" href="#venv.create" title="永久链接至目标">¶</a></dt>
<dd><p>通过关键词参数来创建一个 <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a>，并且使用 <em>env_dir</em> 参数来调用它的 <a class="reference internal" href="#venv.EnvBuilder.create" title="venv.EnvBuilder.create"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create()</span></code></a> 方法。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>Added the <code class="docutils literal notranslate"><span class="pre">with_pip</span></code> parameter</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>Added the <code class="docutils literal notranslate"><span class="pre">prompt</span></code> parameter</p>
</div>
</dd></dl>

</div>
<div class="section" id="an-example-of-extending-envbuilder">
<h2>一个扩展 <code class="docutils literal notranslate"><span class="pre">EnvBuilder</span></code> 的例子<a class="headerlink" href="#an-example-of-extending-envbuilder" title="永久链接至标题">¶</a></h2>
<p>下面的脚本展示了如何通过实现一个子类来扩展 <a class="reference internal" href="#venv.EnvBuilder" title="venv.EnvBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">EnvBuilder</span></code></a>。这个子类会安装 setuptotols 和 pip 的到被创建的虚拟环境中。</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">Popen</span><span class="p">,</span> <span class="n">PIPE</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Thread</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlparse</span>
<span class="kn">from</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="n">urlretrieve</span>
<span class="kn">import</span> <span class="nn">venv</span>

<span class="k">class</span> <span class="nc">ExtendedEnvBuilder</span><span class="p">(</span><span class="n">venv</span><span class="o">.</span><span class="n">EnvBuilder</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This builder installs setuptools and pip so that you can pip or</span>
<span class="sd">    easy_install other packages into the created virtual environment.</span>

<span class="sd">    :param nodist: If true, setuptools and pip are not installed into the</span>
<span class="sd">                   created virtual environment.</span>
<span class="sd">    :param nopip: If true, pip is not installed into the created</span>
<span class="sd">                  virtual environment.</span>
<span class="sd">    :param progress: If setuptools or pip are installed, the progress of the</span>
<span class="sd">                     installation can be monitored by passing a progress</span>
<span class="sd">                     callable. If specified, it is called with two</span>
<span class="sd">                     arguments: a string indicating some progress, and a</span>
<span class="sd">                     context indicating where the string is coming from.</span>
<span class="sd">                     The context argument can have one of three values:</span>
<span class="sd">                     &#39;main&#39;, indicating that it is called from virtualize()</span>
<span class="sd">                     itself, and &#39;stdout&#39; and &#39;stderr&#39;, which are obtained</span>
<span class="sd">                     by reading lines from the output streams of a subprocess</span>
<span class="sd">                     which is used to install the app.</span>

<span class="sd">                     If a callable is not specified, default progress</span>
<span class="sd">                     information is output to sys.stderr.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;nodist&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">nopip</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;nopip&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">progress</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;progress&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">post_setup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set up any packages which need to be pre-installed into the</span>
<span class="sd">        virtual environment being created.</span>

<span class="sd">        :param context: The information for the virtual environment</span>
<span class="sd">                        creation request being processed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s1">&#39;VIRTUAL_ENV&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">env_dir</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">install_setuptools</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
        <span class="c1"># Can&#39;t install pip without setuptools</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nopip</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">nodist</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">install_pip</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">reader</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">stream</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Read lines from a subprocess&#39; output stream and either pass to a progress</span>
<span class="sd">        callable (if specified) or write progress information to sys.stderr.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">progress</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress</span>
        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">stream</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">s</span><span class="p">:</span>
                <span class="k">break</span>
            <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">progress</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">context</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
                <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
        <span class="n">stream</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">install_script</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">url</span><span class="p">):</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">urlparse</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
        <span class="n">fn</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">path</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">binpath</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="n">bin_path</span>
        <span class="n">distpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">binpath</span><span class="p">,</span> <span class="n">fn</span><span class="p">)</span>
        <span class="c1"># Download script into the virtual environment&#39;s binaries folder</span>
        <span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">distpath</span><span class="p">)</span>
        <span class="n">progress</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">progress</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span><span class="p">:</span>
            <span class="n">term</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">term</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">progress</span><span class="p">(</span><span class="s1">&#39;Installing </span><span class="si">%s</span><span class="s1"> ...</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">term</span><span class="p">),</span> <span class="s1">&#39;main&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;Installing </span><span class="si">%s</span><span class="s1"> ...</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">term</span><span class="p">))</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
        <span class="c1"># Install in the virtual environment</span>
        <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="n">context</span><span class="o">.</span><span class="n">env_exe</span><span class="p">,</span> <span class="n">fn</span><span class="p">]</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">stderr</span><span class="o">=</span><span class="n">PIPE</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">binpath</span><span class="p">)</span>
        <span class="n">t1</span> <span class="o">=</span> <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reader</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="s1">&#39;stdout&#39;</span><span class="p">))</span>
        <span class="n">t1</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">t2</span> <span class="o">=</span> <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">reader</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">stderr</span><span class="p">,</span> <span class="s1">&#39;stderr&#39;</span><span class="p">))</span>
        <span class="n">t2</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
        <span class="n">p</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
        <span class="n">t1</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="n">t2</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">progress</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">progress</span><span class="p">(</span><span class="s1">&#39;done.&#39;</span><span class="p">,</span> <span class="s1">&#39;main&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;done.</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="c1"># Clean up - no longer needed</span>
        <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">distpath</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">install_setuptools</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Install setuptools in the virtual environment.</span>

<span class="sd">        :param context: The information for the virtual environment</span>
<span class="sd">                        creation request being processed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">install_script</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="s1">&#39;setuptools&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>
        <span class="c1"># clear up the setuptools archive which gets downloaded</span>
        <span class="n">pred</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">o</span><span class="p">:</span> <span class="n">o</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;setuptools-&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">o</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.tar.gz&#39;</span><span class="p">)</span>
        <span class="n">files</span> <span class="o">=</span> <span class="nb">filter</span><span class="p">(</span><span class="n">pred</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">bin_path</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">files</span><span class="p">:</span>
            <span class="n">f</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">bin_path</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
            <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">install_pip</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Install pip in the virtual environment.</span>

<span class="sd">        :param context: The information for the virtual environment</span>
<span class="sd">                        creation request being processed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">url</span> <span class="o">=</span> <span class="s1">&#39;https://raw.github.com/pypa/pip/master/contrib/get-pip.py&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">install_script</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="s1">&#39;pip&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">compatible</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">):</span>
        <span class="n">compatible</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">elif</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">sys</span><span class="p">,</span> <span class="s1">&#39;base_prefix&#39;</span><span class="p">):</span>
        <span class="n">compatible</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">compatible</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;This script is only for use with &#39;</span>
                         <span class="s1">&#39;Python 3.3 or later&#39;</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="kn">import</span> <span class="nn">argparse</span>

        <span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">prog</span><span class="o">=</span><span class="vm">__name__</span><span class="p">,</span>
                                         <span class="n">description</span><span class="o">=</span><span class="s1">&#39;Creates virtual Python &#39;</span>
                                                     <span class="s1">&#39;environments in one or &#39;</span>
                                                     <span class="s1">&#39;more target &#39;</span>
                                                     <span class="s1">&#39;directories.&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;dirs&#39;</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;ENV_DIR&#39;</span><span class="p">,</span> <span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;+&#39;</span><span class="p">,</span>
                            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;A directory in which to create the</span>
                                 <span class="s1">&#39;virtual environment.&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--no-setuptools&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                            <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;nodist&#39;</span><span class="p">,</span>
                            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Don&#39;t install setuptools or pip in the &quot;</span>
                                 <span class="s2">&quot;virtual environment.&quot;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--no-pip&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                            <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;nopip&#39;</span><span class="p">,</span>
                            <span class="n">help</span><span class="o">=</span><span class="s2">&quot;Don&#39;t install pip in the virtual &quot;</span>
                                 <span class="s2">&quot;environment.&quot;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--system-site-packages&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                            <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;system_site&#39;</span><span class="p">,</span>
                            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Give the virtual environment access to the &#39;</span>
                                 <span class="s1">&#39;system site-packages dir.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;nt&#39;</span><span class="p">:</span>
            <span class="n">use_symlinks</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">use_symlinks</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--symlinks&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">use_symlinks</span><span class="p">,</span>
                            <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;symlinks&#39;</span><span class="p">,</span>
                            <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Try to use symlinks rather than copies, &#39;</span>
                                 <span class="s1">&#39;when symlinks are not the default for &#39;</span>
                                 <span class="s1">&#39;the platform.&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--clear&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
                            <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;clear&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Delete the contents of the &#39;</span>
                                               <span class="s1">&#39;virtual environment &#39;</span>
                                               <span class="s1">&#39;directory if it already &#39;</span>
                                               <span class="s1">&#39;exists, before virtual &#39;</span>
                                               <span class="s1">&#39;environment creation.&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--upgrade&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
                            <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;upgrade&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Upgrade the virtual &#39;</span>
                                                 <span class="s1">&#39;environment directory to &#39;</span>
                                                 <span class="s1">&#39;use this version of &#39;</span>
                                                 <span class="s1">&#39;Python, assuming Python &#39;</span>
                                                 <span class="s1">&#39;has been upgraded &#39;</span>
                                                 <span class="s1">&#39;in-place.&#39;</span><span class="p">)</span>
        <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;--verbose&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span>
                            <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;verbose&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Display the output &#39;</span>
                                               <span class="s1">&#39;from the scripts which &#39;</span>
                                               <span class="s1">&#39;install setuptools and pip.&#39;</span><span class="p">)</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">upgrade</span> <span class="ow">and</span> <span class="n">options</span><span class="o">.</span><span class="n">clear</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;you cannot supply --upgrade and --clear together.&#39;</span><span class="p">)</span>
        <span class="n">builder</span> <span class="o">=</span> <span class="n">ExtendedEnvBuilder</span><span class="p">(</span><span class="n">system_site_packages</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">system_site</span><span class="p">,</span>
                                       <span class="n">clear</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">clear</span><span class="p">,</span>
                                       <span class="n">symlinks</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">symlinks</span><span class="p">,</span>
                                       <span class="n">upgrade</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">upgrade</span><span class="p">,</span>
                                       <span class="n">nodist</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">nodist</span><span class="p">,</span>
                                       <span class="n">nopip</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">nopip</span><span class="p">,</span>
                                       <span class="n">verbose</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">verbose</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">options</span><span class="o">.</span><span class="n">dirs</span><span class="p">:</span>
            <span class="n">builder</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">rc</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">main</span><span class="p">()</span>
        <span class="n">rc</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Error: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">e</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
    <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">rc</span><span class="p">)</span>
</pre></div>
</div>
<p>这个脚本同样可以 <a class="reference external" href="https://gist.github.com/vsajip/4673395">在线下载</a>。</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code> --- 创建虚拟环境</a><ul>
<li><a class="reference internal" href="#creating-virtual-environments">创建虚拟环境</a></li>
<li><a class="reference internal" href="#api">API</a></li>
<li><a class="reference internal" href="#an-example-of-extending-envbuilder">一个扩展 <code class="docutils literal notranslate"><span class="pre">EnvBuilder</span></code> 的例子</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="ensurepip.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code> --- Bootstrapping the <code class="docutils literal notranslate"><span class="pre">pip</span></code> installer</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="zipapp.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code> --- Manage executable Python zip archives</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/venv.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="zipapp.html" title="zipapp --- Manage executable Python zip archives"
             >下一页</a> |</li>
        <li class="right" >
          <a href="ensurepip.html" title="ensurepip --- Bootstrapping the pip installer"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="distribution.html" >软件打包和分发</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>